package kr.hyosang.android.penfo.parser;

import kr.hyosang.android.penfo.SharedObject;
import kr.hyosang.android.penfo.data.Article;
import kr.hyosang.android.penfo.data.MainPageData;

public class MainPageParser extends Parser<MainPageData>
{
    public MainPageData parse(String html) {
        MainPageData mainData = new MainPageData();
        int nIdx;
        String date, subject, no;
        int nComment;
        
        SharedObject.getInstance().bLogin = isLogin(html);
        
        //글 리스트 부분
        String articlePart = getSubContent(html, "<div id=\"bbs\">", "<div id=\"ads\">");
        
        //Hot Now부분
        String hotNowPart = getSubContent(articlePart, "<span class=main_title>::Hot", "<span class=main_title>");
        nIdx = hotNowPart.indexOf("<span class=\"spanDate\"");
        while(nIdx >= 0) {
            date = getSubContent(hotNowPart, ">", "</span>", nIdx);
            subject = getSubContent(hotNowPart, "&nbsp;", "</a>", nIdx);
            no = getSubContent(subject, "&no=", ">");
            subject = getSubContent(subject, "spanSubject\">", "</span>");
            nComment = getCommentFromSubject(subject);
            subject = stripCommentFromSubject(subject, nComment);
            
            Article article = new Article();
            article.nBoardType = Article.BOARD_HOTNOW;
            article.subject = subject;
            article.date = date;
            article.no = no;
            article.nComment = nComment;
            
            mainData.listHotNow.add(article);
            
            nIdx = hotNowPart.indexOf("<span class=\"spanDate\"", nIdx + 10);
        }
        
        
        //Freeboard
        String freeBoardPart = getSubContent(articlePart, "::Freeboard::", "<span class=main_title>");
        nIdx = freeBoardPart.indexOf("<span class=\"spanDate\"");
        while(nIdx >= 0) {
            date = getSubContent(freeBoardPart, ">", "</span>", nIdx);
            subject = getSubContent(freeBoardPart, "</span>", "</a>", nIdx);
            no = getSubContent(subject, "&no=", ">");
            subject = getSubContent(subject, ">", "");
            nComment = getCommentFromSubject(subject);
            subject = stripCommentFromSubject(subject, nComment);
            
            Article article = new Article();
            article.nBoardType = Article.BOARD_FREEBOARD;
            article.subject = subject;
            article.date = date;
            article.no = no;
            article.nComment = nComment;
            
            mainData.listFreeboard.add(article);
            
            nIdx = freeBoardPart.indexOf("<span class=\"spanDate\"", nIdx + 10);
        }
        
        //Review
        String reviewPart = getSubContent(articlePart, "::Review::", "<span class=main_title>");
        nIdx = reviewPart.indexOf("<span class=\"spanDate\"");
        while(nIdx >= 0) {
            date = getSubContent(reviewPart, ">", "</span>", nIdx);
            subject = getSubContent(reviewPart, "</span>", "</a>", nIdx);
            no = getSubContent(subject, "&no=", ">");
            subject = getSubContent(subject, ">", "");
            nComment = getCommentFromSubject(subject);
            subject = stripCommentFromSubject(subject, nComment);
            
            Article article = new Article();
            article.nBoardType = Article.BOARD_REVIEW;
            article.subject = subject;
            article.date = date;
            article.no = no;
            article.nComment = nComment;
            
            mainData.listReview.add(article);
            
            nIdx = reviewPart.indexOf("<span class=\"spanDate\"", nIdx + 10);
        }
        
        //Q&A
        String qnaPart = getSubContent(articlePart, "::Q & A::", "<span class=main_title>");
        nIdx = qnaPart.indexOf("<span class=\"spanDate\"");
        while(nIdx >= 0) {
            date = getSubContent(qnaPart, ">", "</span>", nIdx);
            subject = getSubContent(qnaPart, "</span>", "</a>", nIdx);
            no = getSubContent(subject, "&no=", ">");
            subject = getSubContent(subject, ">", "");
            nComment = getCommentFromSubject(subject);
            subject = stripCommentFromSubject(subject, nComment);
            
            Article article = new Article();
            article.nBoardType = Article.BOARD_QNA;
            article.subject = subject;
            article.date = date;
            article.no = no;
            article.nComment = nComment;
            
            mainData.listQnA.add(article);
            
            nIdx = qnaPart.indexOf("<span class=\"spanDate\"", nIdx + 10);
        }
        
        //Penfo's choice
        String choiceImage = getSubContent(articlePart, "onmouseover=\"this.src='", "';");
        mainData.choiceImage = choiceImage;
        
        //Featured Pictures
        String features = getSubContent(html, "<div id=\"thumb_gallery\">", "<span id=\"copyright\">");
        nIdx = features.indexOf("onmouseover=\"this.src=");
        while(nIdx >= 0) {
            String uri = getSubContent(features, "'", "';", nIdx);
            
            mainData.featured.add(uri);
            
            nIdx = features.indexOf("onmouseover=\"this.src=", nIdx + 10);
        }
        
        
        return mainData;
    }
}
